
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Locale;
import java.util.Scanner;

public class SocketClient {
    public static void main(String[] args){
        try {
            Socket socket=new Socket(host:"127.0.0.1",port:8888);
            System.out.println("服务器连接成功");

            while (true) {
                System.out.println("请输入发送的内容：");
                Scanner scanner = new Scanner(System.in);
                String nextLine = scanner.nextLine();

                OutputStream outputStream = socket.getOutputStream();
                outputStream.write(nextLine.getBytes());

                InputStream inputStream = socket.getInputStream();
                byte[] bytes =new byte[1024];
                int len;
                while ((len = inputStream.read(bytes))  !=-1){
                    System.out.println("服务器说："+new String(bytes));
                }
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}
